//
//  CYUserBtn.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/29.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYUserBtn.h"
@interface CYUserBtn()
@property (nonatomic, strong) CYLabel *mainLBL;
@property (nonatomic, strong) CYLabel *secLBL;
@property (nonatomic, assign) CGFloat mainFont;
@property (nonatomic, assign) CGFloat secFont;
@property (nonatomic, strong) UIColor *color;
@property (nonatomic, assign) mainTitleLayout layout;
@end
@implementation CYUserBtn
- (instancetype)initWithMainTitle:(NSString *)mainTitle secTitle:(NSString *)secTitle mainFont:(CGFloat)mainFont secFont:(CGFloat)secFont color:(UIColor *)color layout:(mainTitleLayout)layout{
    self = [super init];
    if (self) {
        self.layout = layout;
        self.color = color;
        self.mainFont = mainFont;
        self.secFont = secFont;
        self.mainTitle = mainTitle;
        self.secTitle = secTitle;
        [self sizeToFit];
    }
    return self;
}

- (CYLabel *)mainLBL{
    
    if (!_mainLBL) {
        _mainLBL = [[CYLabel alloc]initWithText:_mainTitle size:_mainFont weight:UIFontWeightBold];
        _mainLBL.textColor = _color;
        [self addSubview:_mainLBL];
    
    [_mainLBL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(0);
        make.size.mas_equalTo(_mainLBL.bounds.size);
        if (_layout == mainTitleLeft)
            make.left.equalTo(self).with.offset(0);
        else if (_layout == mainTitleCenter)
            make.centerX.equalTo(self);
        else if (_layout == mainTitleRight)
            make.right.equalTo(self.mas_right).with.offset(0);
    }];
   }
    return _mainLBL;
}

- (CYLabel *)secLBL{
    
    if (!_secLBL) {
        _secLBL = [[CYLabel alloc]initWithText:_secTitle size:_secFont textcolor:_color];
        [self addSubview:_secLBL];
    
    [_secLBL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(0);
        make.bottom.equalTo(self.mas_bottom).with.offset(0);
        make.size.mas_equalTo(_secLBL.bounds.size);
    }];
    }
    return _secLBL;
}

//主字体
- (void)setMainTitle:(NSString *)mainTitle{
    if (_mainTitle != mainTitle)
        _mainTitle = mainTitle;
    self.mainLBL.text = _mainTitle;
    
    [_mainLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_mainLBL.bounds.size);
    }];
}

//补充字体
- (void)setSecTitle:(NSString *)secTitle{
    if (_secTitle != secTitle)
        _secTitle = secTitle;
    self.secLBL.text = secTitle;
    [_secLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_secLBL.bounds.size);
    }];

}
- (void)sizeToFit{
    
    if (!_mainTitle && !_secTitle)
        return;
    CGFloat width = (_mainLBL.bounds.size.width > _secLBL.bounds.size.width)?_mainLBL.bounds.size.width:_secLBL.bounds.size.width;
    self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, width, _mainLBL.bounds.size.height + _secLBL.bounds.size.height + AL(PT(12)));
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
